﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Notepad
{
    public partial class NotepadForm : Form
    {
        public NotepadForm()
        {
            InitializeComponent();
        }

        private void tsmiFont_Click(object sender, EventArgs e)
        {
            FontForm ff = new FontForm();
            ff.FontSave += Ff_FontSave;//注册委托事件
            ff.ShowDialog();//显示模式窗体 线程阻塞

            //this.txtContent.Font = ff.font;
        }

        private void Ff_FontSave(Font font)
        {
            this.txtContent.Font = font;
        }

        private void NotepadForm_Load(object sender, EventArgs e)
        {
            //this.txtContent.Font = new Font("宋体",20f);//固定样式
            //GetFont获取字体信息
            this.txtContent.Font = Config.GetFont();
        }

        private void tsmiSave_Click(object sender, EventArgs e)
        {
            Save();
        }
        /// <summary>
        /// 保存方法
        /// </summary>
        private void Save()
        {
            //持久化的时候选择一个指定的地方 SaveFileDialog用户保存文件位置
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "(*.txt)|*txt";//Filter过滤格式
            DialogResult result = sfd.ShowDialog();
            if (result == DialogResult.OK)//判断是否保存
            {
                //文件持久化,写入到磁盘(路径，保存内容)
                File.WriteAllText(sfd.FileName, this.txtContent.Text);
            }
        }


        //自定义组合事件 第一步:把KeyPrview=属性true 第二步：KeyDown注册一个窗体按键事件
        private void NotepadForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.S)
            {
                Save();//调用Save
            }
        }

        /// <summary>
        /// 打开事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiOpen_Click(object sender, EventArgs e)
        {
            //OpenFileDialog提示用户打开文件
            OpenFileDialog ofd = new OpenFileDialog();
            DialogResult result = ofd.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.txtContent.Text = File.ReadAllText(ofd.FileName);
            }
        }
        /// <summary>
        /// 字体颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiColor_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            DialogResult result = cd.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.txtContent.ForeColor = cd.Color;
            }
        }
    }
}
